package com.nulldev.util.java.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.nulldev.util.analytics.nAnalyticsSettingsUI;
import com.nulldev.util.analytics.v2.nAnalytics2Configurator;
import com.nulldev.util.analytics.v2.nAnalytics2PermissionPrompt;
import com.nulldev.util.java.swing.extSwing.eFontPicker;
import com.nulldev.util.java.swing.extSwing.eListCellRenderer;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.Theme;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.ThemeApplier;
import com.nulldev.util.java.swing.font.FontManager;
import com.nulldev.util.java.swing.vdesktop.vDesktop_Main;
import com.nulldev.util.other.ArrayUtils;

import javax.swing.JTabbedPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JSpinner.DefaultEditor;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.SwingConstants;

public class nullUtilAbout2 extends JFrame {

	private static nullUtilAbout2 instance;
	private JPanel contentPane;

	public static void main(String[] args) {
		FontManager.loadSystemSpecificFonts();
		main();
	}
	
	/**
	 * Launch the application.
	 */
	public static void main() {
		if (instance != null) {
			instance.toggle();
			return;
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					nullUtilAbout2 frame = new nullUtilAbout2();
					ThemeApplier.applyTheme(frame);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public nullUtilAbout2() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(nullUtilAbout2.class.getResource("/com/nulldev/util/java/swing/nullutil_logo.png")));
		this.instance = this;
		setTitle("nullUtil 2.6.0-beta: About");
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 592, 359);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("About", null, panel, null);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(nullUtilAbout2.class.getResource("/com/nulldev/util/java/swing/nullutil_logo.png")));
		
		JLabel lblNullutilBy = new JLabel("nullUtil. by <null>");
		
		JLabel lblVersion = new JLabel("version 2.6.0-beta");
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel lblModules = new JLabel("Modules:");
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
						.addComponent(label)
						.addComponent(lblNullutilBy)
						.addComponent(lblVersion)
						.addComponent(lblModules))
					.addContainerGap())
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(label)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblNullutilBy)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblVersion)
					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(lblModules)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		
		JList list = new JList() {
			@Override
			public void paintComponent(final Graphics g) {
				((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				super.paintComponent(g);
			}
		};
		list.setModel(new AbstractListModel() {
			String[] values = new String[] {"nAnalytics (from 2.4, 03/2018)", "nAnalytics v2 (from 2.5.1, 05/2018)", "nAudio (from 2.0, 2017 - moved to nAV)", "nAudio2 (from 2.4.2, 04/2018 - moved to nAV)", "nAV (nAudioVideo) (from 2.5.2, 06/2018)", "nCrypto (1.0 from 2016 | 2.0 + 3.0 from 2.2, 2018 - removed from 2.5.1)", "nCryptoAPI (from 2.5.1, 06/2018)", "genericUtils (from 1.0, 2016)", "imageUtil (from 2.1, 2016-2017)", "swingUtils (from 2.3, end of 2017)", "extSwing (early from 2.3, end of 2017 | full from 2.4, 2018)", "swingCanvas (from 2.1, early 2017)", "JVMUtils (from 2.3, end of 2017)", "nAPI (from 2.4, 03/2018)", "netUtil (from 1.0, 2016 | from 2.0 | 2017)", "hardwareUtil (from 2.3, end of 2017) ", "processAPI (from 2.3, end of 2017)", "URIapi (from 2.2, 2017)", "iWebKit (from 2.3, end of 2017, deprecated in 2.4)", "extJava (from 2.2, 2017)", "prefAPI (from 2.4, 03/2018)", "threadIt! (from 2.0, 2017)"};
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		scrollPane.setViewportView(list);
		panel.setLayout(gl_panel);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("Global Font", null, panel_1, null);
		
		JLabel lblFont = new JLabel("Font:");
		
		JComboBox<String> comboBox = new JComboBox<String>() {
			@Override
			public void paintComponent(final Graphics g) {
				((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				super.paintComponent(g);
			}
		};
		comboBox.setEnabled(false);
		comboBox.setRenderer(new eListCellRenderer());
		final JSpinner spinner = new JSpinner();
		spinner.setEnabled(false);
		spinner.setModel(new SpinnerNumberModel(16, 8, 32, 1));
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (spinner.getValue() == null || (int) spinner.getValue() <= 2) {
					return;
				}
				//FontManager.setFont = FontManager.getFont((String) comboBox.getSelectedItem()).deriveFont((int) spinner.getValue());
				FontManager.setFont(FontManager.getFont((String) comboBox.getSelectedItem()).deriveFont((int) spinner.getValue()), (int) spinner.getValue());
				FontManager.applyAll(FontManager.getFontSet());
			}
		});
		spinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				if (spinner.getValue() == null || (int) spinner.getValue() <= 2) {
					return;
				}
				FontManager.setFont(FontManager.getFont((String) comboBox.getSelectedItem()).deriveFont((int) spinner.getValue()), (int) spinner.getValue());
				//FontManager.setFont = FontManager.getFont((String) comboBox.getSelectedItem()).deriveFont((int) spinner.getValue());
				FontManager.applyAll(FontManager.getFontSet());
			}
		});
		((DefaultEditor) spinner.getEditor()).getTextField().setEditable(false);
		final ArrayList<String> llist = FontManager.getGlobalFonts();
		final String[] llistc = ArrayUtils.arrayListToArray(llist);
		final String fn = FontManager.getFontSet().getName()
				.replaceAll("MADEEvolveSans", "MADE Evolve Sans")
				.replaceAll("Traffic 02", "Traffic");
		final int pos = ArrayUtils.get(llistc, fn);
		comboBox.setModel(new DefaultComboBoxModel<String>(llistc));
		comboBox.setSelectedIndex(pos);
		
		JLabel lblFontSize = new JLabel("Font Size:");
		
		JButton btnOpenEfontpicker = new JButton("Open eFontPicker");
		btnOpenEfontpicker.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				eFontPicker.main();
			}
		});
		
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addComponent(comboBox, 0, 551, Short.MAX_VALUE)
						.addComponent(lblFont)
						.addComponent(lblFontSize)
						.addComponent(spinner, GroupLayout.DEFAULT_SIZE, 551, Short.MAX_VALUE)
						.addComponent(btnOpenEfontpicker, GroupLayout.DEFAULT_SIZE, 551, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblFont)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblFontSize)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(spinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 154, Short.MAX_VALUE)
					.addComponent(btnOpenEfontpicker)
					.addContainerGap())
		);
		panel_1.setLayout(gl_panel_1);
		
		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("Theme", null, panel_2, null);
		
		JLabel lblGlobalTheme = new JLabel("Global Theme:");
		
		JComboBox comboBox_1 = new JComboBox() {
			@Override
			public void paintComponent(final Graphics g) {
				((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				super.paintComponent(g);
			}
		};
		comboBox_1.setRenderer(new eListCellRenderer());
		final int pos0 = ArrayUtils.get(Theme.values(), ThemeApplier.defaultTheme);
		comboBox_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				final Theme t = (Theme) comboBox_1.getSelectedItem();
				ThemeApplier.applyAll(t);
			}
		});
		comboBox_1.setModel(new DefaultComboBoxModel(Theme.values()));
		comboBox_1.setSelectedIndex(pos0);
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
						.addComponent(comboBox_1, 0, 549, Short.MAX_VALUE)
						.addComponent(lblGlobalTheme))
					.addContainerGap())
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblGlobalTheme)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(233, Short.MAX_VALUE))
		);
		panel_2.setLayout(gl_panel_2);
		
		JPanel panel_3 = new JPanel();
		tabbedPane.addTab("nAnalytics", null, panel_3, null);
		
		JButton btnLaunchVConfigurator = new JButton("Launch v1 Configurator");
		btnLaunchVConfigurator.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				nAnalyticsSettingsUI.toggle();
			}
		});
		
		JButton btnLaunchVConfigurator_1 = new JButton("Launch v2 Configurator");
		btnLaunchVConfigurator_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				nAnalytics2Configurator.toggle();
			}
		});
		
		JLabel lblNoteTheseMay = new JLabel("NOTE: These may or may not be in use.");
		lblNoteTheseMay.setHorizontalAlignment(SwingConstants.CENTER);
		
		JLabel lblWarningTheseButtons = new JLabel("WARNING: These buttons may have to be clicked on twice.");
		lblWarningTheseButtons.setHorizontalAlignment(SwingConstants.CENTER);
		
		JButton btnLaunchVPermission = new JButton("Launch v2 Permission Prompt");
		btnLaunchVPermission.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				nAnalytics2PermissionPrompt.main();
			}
		});
		GroupLayout gl_panel_3 = new GroupLayout(panel_3);
		gl_panel_3.setHorizontalGroup(
			gl_panel_3.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_3.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_3.createParallelGroup(Alignment.LEADING)
						.addComponent(btnLaunchVConfigurator_1, GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE)
						.addComponent(btnLaunchVConfigurator, GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE)
						.addComponent(lblNoteTheseMay, GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE)
						.addComponent(lblWarningTheseButtons, GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE)
						.addComponent(btnLaunchVPermission, GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_panel_3.setVerticalGroup(
			gl_panel_3.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_3.createSequentialGroup()
					.addContainerGap()
					.addComponent(btnLaunchVConfigurator)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnLaunchVConfigurator_1)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnLaunchVPermission)
					.addPreferredGap(ComponentPlacement.RELATED, 143, Short.MAX_VALUE)
					.addComponent(lblWarningTheseButtons)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblNoteTheseMay)
					.addContainerGap())
		);
		panel_3.setLayout(gl_panel_3);
		
		JPanel panel_4 = new JPanel();
		tabbedPane.addTab("vDesktop", null, panel_4, null);
		
		JButton btnLaunchVdesktop = new JButton("Launch vDesktop");
		btnLaunchVdesktop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				vDesktop_Main.main(new String[0]);
			}
		});
		
		JLabel lblNoteVdesktopIs = new JLabel("NOTE: vDesktop is in early development.");
		lblNoteVdesktopIs.setHorizontalAlignment(SwingConstants.CENTER);
		
		JLabel lblWarningClosingVdesktop = new JLabel("WARNING: Closing vDesktop will close the whole program.");
		lblWarningClosingVdesktop.setHorizontalAlignment(SwingConstants.CENTER);
		GroupLayout gl_panel_4 = new GroupLayout(panel_4);
		gl_panel_4.setHorizontalGroup(
			gl_panel_4.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_4.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNoteVdesktopIs, GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE)
						.addComponent(btnLaunchVdesktop, GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE)
						.addComponent(lblWarningClosingVdesktop, GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_panel_4.setVerticalGroup(
			gl_panel_4.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_4.createSequentialGroup()
					.addContainerGap(205, Short.MAX_VALUE)
					.addComponent(lblWarningClosingVdesktop)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblNoteVdesktopIs)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(btnLaunchVdesktop)
					.addContainerGap())
		);
		panel_4.setLayout(gl_panel_4);
	}
	
	public void toggle() {
		this.setVisible(!this.isVisible());
	}
}
